var API = require('API');

module.exports = ['$scope','$rootScope','$location','$routeParams','$timeout',
function($scope, $rootScope, $location, $routeParams, $timeout){
    
    // user模块
    $scope.user = {};
    
    $scope.user.user = {};
    $scope.user.all_shop = {};
    $scope.user.params = {
        new:{},
        modify:{},
        users:{}
    };
    
    $scope.user.handler = {};

    // 初始化tab
    $scope.user_user_tab = true;
    function tab_init(){
        $scope.user_user_tab = false;
        $scope.user_privileges_tab = false;
        $scope.user_view_tab = false;
    };

    // 初始化overlay
    $scope.user.handler.init_overlay = function(){
        $scope.user.overlay = {
            new:false,
            modify:false,
            users:false,
            handler:false
        };
        $scope.view_shop_ovrlay = false;
        $scope.privileges_ovrlay = false;
    };
    $scope.user.handler.init_overlay();

    // 获取所有门店信息
    $scope.user.handler.get_shop = function(){
        API.shop.all(function (data) {
            $scope.$apply(function () {
                $scope.user.all_shop = data;
                $scope.all_view_shops = data.map(function(i){return i.name});

            })
        })
    };
    $scope.user.handler.get_shop();
    
    // 获取所有用户
    $scope.all_shop = {};
    $scope.user.handler.get_user = function(){
        API.user.brief(function (data) {
            $scope.$apply(function(){
                $scope.user.params.users = data;
                $scope.user.params.users.forEach(function(data){
                    var name = data.shop_name;
                    var shop = $scope.all_shop[name];
                    if (!shop) {
                        $scope.all_shop[name] = [];
                    }
                    $scope.all_shop[name].push(data);
                })
                console.log($scope.all_shop);
            });
        });
    };
    $scope.user.handler.get_user();

    // 用户新增--打开
    $scope.user.handler.new_open = function(){
        $scope.user.overlay.modify = false;
        $scope.user.overlay.new = !$scope.user.overlay.new;
    };
    // 用户新增--提交数据
    $scope.user.handler.new_submit = function(){
        API.user.add($scope.user.params.new, function (data) {
            $scope.user.params.new = {};
            $scope.user.overlay.new = !$scope.user.overlay.new;
        });  
    };
    // 用户修改--打开
    $scope.user.handler.modify_open = function(){
        if ($scope.user.user && $scope.user.user.uid) {
            $scope.user.overlay.new = false;
            $scope.user.overlay.modify = !$scope.user.overlay.modify;
        }else{
            alert('请先选择用户！');
            $scope.user.handler.users_open();
        }
    };
    // 用户修改--提交数据
    $scope.user.handler.modify_submit = function(){
        API.user.upd($scope.user.params.modify, function (data) {
            $scope.user.params.modify = {};
            $scope.user.overlay.modify = !$scope.user.overlay.modify;
        });  
    };
    // 用户列表--打开
    $scope.user.handler.users_open = function(){
        $scope.user.handler.init_overlay();
        $scope.user.overlay.users = true;
        tab_init();
        $scope.user_user_tab = true;
    };
    // 选择用户
    $scope.user.handler.user_item = function(item){
        API.user.get(item.uid,function(data){
            $scope.$apply(function(){
                $scope.user.user = data;
                $scope.user.params.modify = data;
            });
        });
        $scope.user.overlay.handler = true;
    };







// 岗位模块
    var api_user       = require('API').user,
        api_rank       = require('API').rank,
        roles_for_user = require('API').rbac.roles_for_user,
        rbac_reload    = require('API').rbac.reload;

    $scope.all_ranks = [];

    (function () {
        api_rank.all(function (json) {
            $scope.$apply(function () {
                $scope.all_ranks = json;
            });
        });
    })();

    function refresh(cb) {
        $scope.cur_user = {};
        $scope.cur_user_unassigned_ranks = [];
        $scope.cur_user_assigned_ranks = [];
        $scope.cur_user_roles = [];
        if (cb)cb();
    }

    refresh();

    
    $scope.user_ranks_show = function (item) {
        if ($scope.user.user && $scope.user.user.uid) {
            $scope.user.handler.init_overlay();
            $scope.privileges_ovrlay = true;
            $scope.cur_user = $scope.user.user;
            tab_init();
            $scope.user_privileges_tab = true;
        }else{
            alert('请先选择用户！');
            $scope.user.handler.init_overlay();
            $scope.user.handler.users_open();
        }
        api_rank.ranks_for_user($scope.cur_user, function (cur_ranks) {
            roles_for_user($scope.cur_user.id, function (roles) {
                $scope.$apply(function () {
                    $scope.cur_user_roles = roles;
                    $scope.cur_user_assigned_ranks = cur_ranks;
                    $scope.cur_user_unassigned_ranks =
                        $scope.all_ranks.slice(0).filter(function (r) {
                            return !cur_ranks.some(function (x) {
                                return x.id == r.id
                            })
                        });
                })
            });
        });

    };

    $scope.add_rank = function (rank) {
        var save = $scope.cur_user;
        console.log('rank_to_add', rank);
        api_rank.add_rank_to_user(rank, save, function () {
            $scope.$apply(function () {
                refresh(function () {
                    $scope.user_ranks_show(save);
                });
            });
        });
    };

    $scope.remove_rank = function (rank) {
        var save = $scope.cur_user;
        console.log('rank_to_del', rank);
        api_rank.remove_rank_from_user(rank, save, function () {
            $scope.$apply(function () {
                refresh(function () {
                    $scope.user_ranks_show(save);
                });
            });
        });
    };


    // $scope.submit = function () {
    //     var save = $scope.cur_user;
    //     rbac_reload(function(){
    //         $scope.$apply(function () {
    //             refresh(function () {
    //                 $scope.user_ranks_show(save);
    //             });
    //         });
    //     })
    // }















// 可视门店 模块
    $scope.user_view_shops = [];
    $scope.user_view_shops_null = [];

    $scope.view_shop_handler = function(){
        if ($scope.user.user && $scope.user.user.uid) {
            $scope.user.handler.init_overlay();
            $scope.view_shop_ovrlay = true;
            $scope.user_view_shops = ($scope.user.user._view_shops || "").split(',');
            $scope.user_view_shops_null = Array.prototype.slice.call($scope.all_view_shops, 0)
            .filter(function (brand) {
                return $scope.user_view_shops.indexOf(brand) == -1;
            });
            if ($scope.user_view_shops.length == 1 &&$scope.user_view_shops[0] == '') {
                $scope.user_view_shops = [];
            }
            tab_init();
            $scope.user_view_tab = true;
        }else{
            alert('请先选择用户！');
            $scope.user.handler.init_overlay();
            $scope.user.handler.users_open();
        }
       
    };

    // 去除用户已存在可视门店
    $scope.view_shop_detach = function(selected){
        var rm_idx = $scope.user_view_shops.indexOf(selected);
        if(selected != -1){
            $scope.user_view_shops.splice(rm_idx,1);
        }
        
    };
    // 分配用户可视门店
    $scope.view_shop_attach = function(selected){
        var rm_idx = $scope.user_view_shops_null.indexOf(selected);
        if(selected != -1){
            // $scope.user_view_shops_null.splice(rm_idx,1);
            $scope.user_view_shops.unshift(selected);
        }
    };

    $scope.$watch('user_view_shops.length',function(_new,old){
        if (_new !== old) {
            var param = $.extend({}, $scope.user.user);
            param._view_shops = " "+ $scope.user_view_shops.join(',');
            console.log('param', param._view_shops);

// =======
//             var param = $.extend({},$scope.user.user,{
//                 _view_shops : ""+$scope.user_view_shops.join(',')
//             });
//             console.log(param);
// >>>>>>> Stashed changes
            API.user.upd(param, function(){
                API.user.get(param.uid,function(data){
                    $scope.$apply(function(){
                        $scope.user_view_shops = (data._view_shops || "").trim().split(',');
                        $scope.user_view_shops_null = Array.prototype.slice.call($scope.all_view_shops, 0)
                        .filter(function (brand) {
                            return $scope.user_view_shops.indexOf(brand) == -1;
                        });
                        if ($scope.user_view_shops.length == 1 &&$scope.user_view_shops[0] == '') {
                            $scope.user_view_shops = [];
                            console.log($scope.user_view_shops);
                        }

                    });
                });
            })
        }
    });

    // $scope.$watch('user_view_shops.length',function(_new,old){
    //     var t = $scope.user_view_shops.join(',');;
    //     console.log(t,!!t);

    //     if (_new !== old) {

    //         var param = $.extend({},$scope.user.user,{
    //             _view_shops : ""+$scope.user_view_shops.join(',')
    //         });
    //         console.log(param);
    //         API.user.upd(param, function(){
    //             API.user.get(param.uid,function(data){
    //                 $scope.$apply(function(){
    //                     $scope.user_view_shops = (data._view_shops || "").split(',');
    //                     $scope.user_view_shops_null = Array.prototype.slice.call($scope.all_view_shops, 0)
    //                     .filter(function (brand) {
    //                         return $scope.user_view_shops.indexOf(brand) == -1;
    //                     });
    //                     if ($scope.user_view_shops.length == 1 &&$scope.user_view_shops[0] == '') {
    //                         $scope.user_view_shops = [];
    //                         console.log($scope.user_view_shops);
    //                     }

    //                 });
    //             });
    //         })
    //     };
    // });




}]